import { ElMessage, ElLoading } from 'element-plus'
import useUserStore from '../stores/user'
import axiosCreate from './axios-create'

const base = import.meta.env.VITE_APP_BASE
const apiUrl = import.meta.env.VITE_APP_API || '/'

const http = axiosCreate({
  tokenKey: 'Authorization',
  axiosRequestConfig: {
    baseURL: apiUrl,
    // 成功消息提示
    successMsg: false,
    // 错误消息提示
    errorMsg: true,
    // 是否需要登录
    needLogin: true,
    // loading
    showLoading: false,
    // headers
    headers: {},
    // 携带cookie
    // withCredentials: true,
    // 允许跨越
    // crossDomain:true,
  },
  showLoading(config) {
    config.loading = ElLoading.service({
      lock: true,
      text: 'Loading',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.7)',
    })

    // Toast.loading({
    //     duration: 0, // 持续展示 toast
    //     forbidClick: true,
    //     message: typeof showLoading == 'string' ? showLoading : '加载中...',
    // });
  },
  closeLoading(config) {
    if (config.loading) config.loading.close()
  },
  goLogin() {
    const user = useUserStore()
    // user.logout()
    user.reLogin()
  },
  message(type, msg) {
    if (type == 'success') {
      ElMessage.success(msg)
    } else {
      ElMessage.error(msg)
    }
  },
  getToken() {
    return localStorage.token
  },
})

export default http
